{extend name="public:base"}
{block name="body"}
<style>
    .upload_img_list{margin-top: 10px;}
    .upload_img_list > .img_list{display: inline-block;position: relative;margin-right:5px;background-color:#f9f9f9;border:1px solid #efefef;}
    .upload_img_list > .img_list > span{position:absolute;right:5px; top:5px; display: block; background-color:#f56c6c;height:20px;line-height: 20px;color:#fff;}
</style>
<body>
		<form class="layui-form layui-form-pane" action="">
			<div class="mainBox">
				<div class="main-container">
                   <div class="layui-row">
                        <div class="layui-col-xs12 layui-col-sm6 layui-col-md4">
                            <div class="layui-form-item">
                                <label class="layui-form-label">产品标题</label>
                                <div class="layui-input-block">
                                    <input type="text" name="title" value="{$info.title|default=''}" lay-verify="required" autocomplete="off"
                                        placeholder="请输入标题" class="layui-input">
                                </div>
                            </div>
                        </div>
                        <div class="layui-col-xs12 layui-col-sm6 layui-col-md4">
                            <div class="layui-form-item">
                                <label class="layui-form-label">上级分类</label>
                                <div class="layui-input-block">
                                    <select name="cate_id" lay-verify="require">
                                        {volist name="category" id="vo"}
                                        <option value="{$vo.id}" {present name="info.cate_id"} {eq name="info.cate_id" value="$vo['id']"} selected {/eq} {/present}>{$vo.showName}</option>
                                        {/volist}
                                    </select>
                                </div>
                            </div>
                        </div>
                        <div class="layui-col-xs12 layui-col-sm6 layui-col-md4">
                            <div class="layui-form-item">
                                <label class="layui-form-label">产品标签</label>
                                <div class="layui-input-block">
                                    <div class="fairy-tag-container">
                                        <input type="text" autocomplete="off" placeholder="添加标签" class="fairy-tag-input tags">
                                    </div>
                                </div>
                                <input type="hidden" name="tags" id="tags" value="{$info.tags|default=''}">
                            </div>
                        </div>
                        <div class="layui-col-xs12 layui-col-sm6 layui-col-md4">
                            <div class="layui-form-item">
                                <label class="layui-form-label">价格描述</label>
                                <div class="layui-input-block">
                                    <input type="text" name="price" value="{$info.price|default=''}" lay-verify="required" autocomplete="off"
                                        placeholder="99元起" class="layui-input">
                                </div>
                            </div>
                        </div>
                        <div class="layui-col-xs12 layui-col-sm6 layui-col-md4">
                            <div class="layui-form-item">
                                <label class="layui-form-label">产品推荐</label>
                                <div class="layui-input-block">
                                    <input type="checkbox" name="is_index" title="首推" value="1" {present name="info.is_index"} {eq name="info.is_index" value='1'} checked {/eq}{/present}>
                                    <input type="checkbox" name="is_hot" title="热荐" value="1" {present name="info.is_hot"} {eq name="info.is_hot" value='1'} checked {/eq}{/present}> 
                                    <input type="checkbox" name="is_new" title="新品" value="1" {present name="info.is_new"} {eq name="info.is_new" value='1'} checked {/eq}{/present}> 
                                </div>
                            </div>
                        </div>
                        
                        <div class="layui-col-xs12 layui-col-sm6 layui-col-md4">
                            <div class="layui-form-item">
                                <label class="layui-form-label">排序</label>
                                <div class="layui-input-block">
                                    <input type="text" name="sort" value="{$info.sort|default='100'}"  autocomplete="off" placeholder="请输入数字" class="layui-input">
                                </div>
                            </div>
                        </div>
                       
                        <div class="layui-col-xs12 layui-col-sm6 layui-col-md3">
                            <div class="layui-form-item">
                                <label class="layui-form-label">缩略图</label>
                                <div class="layui-input-block" style="left:5px;">
                                     <button class="pear-btn pear-btn-primary" type="button" id="thumbBtn">图片上传</button>
                                    <div class="upload_img_list" >
                                        <div class="img_list" id="upload_box_thumb">
                                            <img src="{$info.thumb|default='/assets/library/admin/images/no_img.png'}" style="width:150px;max-width:150px" />
                                            <input type="hidden" name="thumb" value="{$info.thumb|default=''}">
                                        </div>
                                        
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="layui-col-xs12 layui-col-sm6 layui-col-md3">
                            <div class="layui-form-item">
                                <label class="layui-form-label">图标</label>
                                <div class="layui-input-block" style="left:5px;">
                                     <button class="pear-btn pear-btn-primary" type="button" id="iconBtn">图片上传</button>
                                    <div class="upload_img_list" >
                                        <div class="img_list" id="upload_box_icon">
                                            <img src="{$info.icon|default='/assets/library/admin/images/no_img.png'}" style="width:50px;max-width:50px" />
                                            <input type="hidden" name="icon" value="{$info.icon|default=''}">
                                        </div>
                                        
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="layui-col-xs12 layui-col-sm6 layui-col-md6">
                            <div class="layui-form-item">
                                <label class="layui-form-label">产品图组</label>
                                <div class="layui-input-block" style="left:5px;">
                                     <button class="pear-btn pear-btn-primary" type="button" id="imgBtn">图片上传</button>
                                    <div class="upload_img_list" id="upload_box_images">
                                        {present name="info.images"}
                                        {foreach $info['images'] as $k=>$v }
                                        <div class="img_list">
                                            <span>删除</span>
                                            <img src="{$v}" style="width:150px;max-width:150px" />
                                            <input type="hidden" name="images[]" value="{$v}">
                                        </div>
                                        {/foreach}
                                        {else/} 
                                        <div class="img_list">
                                            <img src="/assets/library/admin/images/no_img.png" style="width:150px;max-width:150px" />
                                            <input type="hidden" name="images[]" value="">
                                        </div>
                                        {/present}
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="layui-col-xs12 layui-col-sm12 layui-col-md12">
                            <div class="layui-form-item">
                                <label class="layui-form-label">产品描述</label>
                                <div class="layui-input-block">
                                    <input type="text" name="description" value="{$info.description|default=''}"  autocomplete="off" placeholder="请输入描述" class="layui-input">
                                </div>
                                
                            </div>
                        </div>

                        <div class="layui-col-xs12 layui-col-sm12 layui-col-md12">
                            <div class="layui-form-item">
                                <label class="layui-form-label">产品介绍</label>
                                <div class="layui-input-block">
                                    <textarea name="content" id="content">{$info.content|default=''}</textarea>
                                </div>
                            </div>
                        </div>
                        
                   </div>
                </div>
            </div>
            <div class="bottom">
            <div class="button-container">
                <input type="hidden" name="id" value="{$info.id|default=''}">
                <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit=""
                    lay-filter="form-save">
                    <i class="layui-icon layui-icon-ok"></i>
                    提交
                </button>
                <button type="reset" class="pear-btn pear-btn-sm">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
            </div>
		</form>
	</body>
{/block}
{block name="script"}
<script>
    layui.use(['jquery','form','inputTag','tinymce','upload','select'], function() {
        let form = layui.form;
        let $ = layui.jquery;
        let inputTag = layui.inputTag;
        let tinymce = layui.tinymce;
        let upload =layui.upload;
        let upload_box = $('#upload_box_images'); 
        let select = layui.select;
        layui.upload.render({
			elem: '#thumbBtn'
			, url: '{:url("ajax/upload")}'
			, done: function (res) {
				//如果上传失败
				if (res.code == 0) {
					return layer.msg('上传失败');
				}
				//上传成功
				
				$('#upload_box_thumb input').attr('value', res.data);		
				$('#upload_box_thumb img').attr('src', res.data);
			}
		});
		layui.upload.render({
			elem: '#iconBtn'
			, url: '{:url("ajax/upload")}'
			, done: function (res) {
				//如果上传失败
				if (res.code == 0) {
					return layer.msg('上传失败');
				}
				//上传成功
				
				$('#upload_box_icon input').attr('value', res.data);		
				$('#upload_box_icon img').attr('src', res.data);
			}
		});
        layui.upload.render({
			elem: '#imgBtn'
			, url: '{:url("ajax/upload")}'
			, done: function (res) {
				//如果上传失败
				if (res.code == 0) {
					return layer.msg('上传失败');
				}
				//上传成功
				if(upload_box.find('.img_list > img').eq(0).attr('src') == '/assets/library/admin/images/no_img.png'){
                    upload_box.find('.img_list').remove();
                }
				$('#upload_box_images').append(
                    '<div class="img_list"><span>删除</span><img src="'+res.data+'" style="width:150px;max-width:150px" /><input type="hidden" name="images[]" value="'+res.data+'"></div>'
                );
			}
		});
        $(document).on('click','.img_list > span',function(){
            let that = $(this);
            
            layer.confirm('确定要删除当前图片吗', {btn: ['确定', '取消'], title: "提示"}, function (index) {
                that.closest('.img_list').remove();
                if(upload_box.find('.img_list').length ==0){
                    $('#upload_box_images').append(
                    '<div class="img_list"><img src="/assets/library/admin/images/no_img.png" style="width:150px;max-width:150px" /><input type="hidden" name="images[]" value=""></div>'
                );
                }
                layer.close(index);
            });
        })
        var tagObj1 = inputTag.render({
            elem: '.tags',
            data: [
                {present name="info.tags"}{foreach $info['tags_arr'] as $k=>$v } "{$v}",{/foreach}{/present}
            ],
            onChange: function (data, value, type){
                $('#tags').val(data?data.join(','):'');
            }
        });
        var edit = tinymce.render({
					elem: "#content",
					height: 600,
                    images_upload_url:"{:url('ajax/upload')}",
                    form:{name:"file"},
                    convert_urls:false,
                    forced_root_block:false,
                    valid_elements : 'img[src|width=max-width:100%],strong/b,div,br,hr,p,span'
				});
        form.on('submit(form-save)', function(data) {
            console.log(edit.getContent());
            $('[name="content"]').html( edit.getContent());
            console.log()
            $.ajax({
                url: '{:url("edit")}',
                data: $('.layui-form').serialize(),
                type: 'post',
                success: function(result) {
                    if (result.code == 1) {
                        layer.msg('操作成功', {
                            icon: 1,
                            time: 1000
                        }, function() {
                            parent.layer.close(parent.layer.getFrameIndex(window.name)); //关闭当前页
                            parent.layui.table.reload("table-list");
                        });
                    } else {
                        layer.msg(result.msg, {
                            icon: 2,
                            time: 1000
                        });
                    }
                }
            })
            return false;
        });
    })
</script>
{/block}
